package com.gxc.dbcore.dao.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;

import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;

public class SqlDaoImpl extends SqlSessionTemplate implements SqlDao {

	public SqlDaoImpl(SqlSessionFactory sqlSessionFactory) {
		super(sqlSessionFactory);
	}

	/**
	 * 查询记录条数
	 * 
	 * @param statement
	 * @param parameter
	 * @return
	 */
	public long selectForCount(String statement, Object parameter) {
		return SqlDaoCountImpl.count(this, statement, parameter);
	}

	/**
	 * 分页查询
	 * 
	 * @param statement
	 * @param parameter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PageList selectPageList(String statement, Object parameter,
			PageInfo pageInfo) {
		PageList pageList = new PageList();
		int recordCount = (int) this.selectForCount(statement, parameter);
		pageInfo.setByCount(recordCount);
		pageList.setPageInfo(pageInfo);
		RowBounds rowBounds = new RowBounds(pageInfo.getOffset(),
				pageInfo.getLimit());
		List list = this.selectList(statement, parameter, rowBounds);
		pageList.setList(list);
		return pageList;
	}

	/**
	 * 分页查询
	 * 
	 * @param statement
	 * @param countStatement
	 * @param parameter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PageList selectPageList(String statement, String countStatement,
			Object parameter, PageInfo pageInfo) {
		PageList pageList = new PageList();
		int recordCount = (Integer) this.selectOne(countStatement, parameter);
		pageInfo.setByCount(recordCount);
		pageList.setPageInfo(pageInfo);
		RowBounds rowBounds = new RowBounds(pageInfo.getOffset(),
				pageInfo.getLimit());
		List list = this.selectList(statement, parameter, rowBounds);
		pageList.setList(list);
		return pageList;
	}

	/**
	 * 分页查询
	 * 
	 * @param statement
	 * @param parameter
	 * @param countStatement
	 * @param countParameter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PageList selectPageList(String statement, Object parameter,
			String countStatement, Object countParameter, PageInfo pageInfo) {
		PageList pageList = new PageList();
		int recordCount = (Integer) this.selectOne(countStatement,
				countParameter);
		pageInfo.setByCount(recordCount);
		pageList.setPageInfo(pageInfo);
		RowBounds rowBounds = new RowBounds(pageInfo.getOffset(),
				pageInfo.getLimit());
		List list = this.selectList(statement, parameter, rowBounds);
		pageList.setList(list);
		return pageList;
	}
}
